<?php
/**
 * Interface to edit a single course
 */
?>
<html>
<head>

</head>
<body>
<?php
require_once '../dbms_university_courses_mvc/Model/model_require_once.php';
?>
<h2>University Courses</h2>
<?php
/* @var $course Course */ 
$course = Course::constructFrom_course_id($_GET['course_id']);

//Setup the Course Javascript objects, these objects are required for updates and delections of the underlying database rows 
$course->instantiate_javascript_object();
?>
<table>
	<tr>
		<th>Name</th>
		<th>Instructor</th>
		<th>Department</th>
		<th>Term</th>
		<th>Credits</th>
		<th>Classroom</th>
		<th>Delete</th>
	</tr>
	<tr>
		<td><?php
	echo $course->get_course_name ();
	?></td>
		<td><?php
	//instead of showing instructor_ids, which are stored in the database row, get the actual instructor object
	//and show the first and last name
	/* @var $instructor Instructor */
	$instructor_id = $course->get_instructor_id ();
	if (isset ( $instructor_id )) {
		$instructor = Instructor::constructFrom_instructor_id ( $instructor_id );
		$profile = Profile::constructFrom_profile_id ( $instructor->get_profile_id () );
		echo $profile->get_first_name () . " " . $profile->get_last_name ();
	}
	?></td>
		<td><?php
	//instead of showing department_ids, which are stored in the database row, get the actual department object
	//and show the department name		
	/* @var $department Department */
	$department_id = $course->get_department_id ();
	if (isset ( $department_id )) {
		$department = Department::constructFrom_department_id ( $department_id );
		echo $department->get_department_name ();
	}
	?></td>
		<td><?php
	//get the term name and year (eg fall 2010)
	/* @var $term Term */
	$term_id = $course->get_term_id ();
	if (isset ( $term_id )) {
		$term = Term::constructFrom_term_id ( $term_id );
		echo $term->get_term () . " " . $term->get_year ();
	}
	?></td>
		<td><?php
	echo $course->get_credits ();
	?></td>
		<td><?php
	echo $course->get_classroom_id ();
	?></td>
		<td>
		<?php $course->print_delete_button(true, "refreshPage");?>
		</td>
	</tr>
	<!-- The prerequisites for this course -->
	<tr>
		<td></td>
		<td colspan="6">
		<h2>Prerequisites</h2>
		</td>
	</tr>
	<?php
	//get all the prereq objects
	/* @var $coursePrereq CoursePrerequisite */
	$coursePrereqs = CoursePrerequisite::getCoursePrerequisites_by_course_id ( $course->get_course_id () );
	//iterate through the course prereqs, and print them to display
	//also make it so that they can be removed from the list of prereqs
	foreach ( $coursePrereqs as $coursePrereq ) {
		//construct the prereq course from the preq course's id
		/* @var $prereqCourse Course */
		$prereqCourse = Course::constructFrom_course_id ( $coursePrereq->get_prerequisite_course_id() );
		?>
		<tr>
		<td></td>
		<td></td>
		<td colspan="2"><em><?php
		echo $prereqCourse->get_course_name ();
		?></em></td>
		<td>
		<?php //print out the javascript object for this single course prereq
		$coursePrereq->instantiate_javascript_object(); 
		$coursePrereq->print_delete_button(true, "refreshPage");
		?>
		</td>
	</tr>
	<tr>
		<td>
<!-- Create the javascript objects for the prereq courses -->
<script type="text/javascript">
		var course<?php
	echo $course->get_course_id ();
	?>_prereq_id;
	function setCourse<?php echo $course->get_course_id() ?>PrereqId(id){
		course<?php
				echo $course->get_course_id ();
				?>_prereq_id = id;
	}
	</script></td>
		<td colspan="6"><?php
		//print out a course dropdown so one can select a course to add as a prereq
		Course::print_Course_dropdown(Course::getCourses(), "course_name", "", "setCourse".$course->get_course_id()."PrereqId");
	?>
<!-- Adds a new course as a prerequisite for the course with the id of $course->get_course_id -->
	<input align="left" class="ui-corner-all ui-state-default"
	type="button"
	onclick="insertNewCoursePrerequisite(<?php echo $course->get_course_id(); ?>, course<?php echo $course->get_course_id (); ?>_prereq_id, refreshPage);"
	value="Add Prereq"
/>
<br><br>
	</td>
	</tr>
	<?php
}

//insert new course via ajax call
?>
</table>
<br>
<br>
<h2>Description</h2>
<?php $course->print_course_description_wysiwyg_setter(); ?>

<script type="text/javascript">
$(
function(){
	pageLoaded();
}
		);
</script>
</body>
</html>